<?php

namespace apiend\modules\user\models;

use Yii;
use yii\data\ActiveDataProvider;
use common\helpers\Url;

class FollowTeacherSearch extends \common\models\teacher\Teacher {

    /**
     * 关注列表
     * @param array $params
     * @return \yii\data\ActiveDataProvider
     */
    public function search($uid) {
        $query = static::find()
        ->where([
            'status' => static::STATUS_ACTIVE,
            'role_id' => static::ROLE_ID_TEACHER,
        ]);
        $query->joinWith('follow f')->andWhere(['f.user_id' => $uid]);
        $query->orderBy([
            'f.listorder' => SORT_ASC,
            'f.created_at' => SORT_DESC,
        ]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        return $dataProvider;
    }

    public function fields() {
        return [
            'id' => function () {
                return (string) $this->id;
            },
            'name',
            'avatar' => function () {
                return Url::toRemote($this->avatar);
            },
            'grade' => function () {
                return (string) $this->grade_id;
            },
            'gender' => function () {
                return (string) $this->gender;
            },
            'nation' => function () {
                if ($this->nation) {
                    return $this->nation->name;
                }
                return '未设置';
            },
            'birthday' => function () {
                if ($this->birthday) {
                    return $this->birthday;
                }
                return '保密';
            },
            'scopes',
            'description' => function () {
                return (string) $this->description;
            },
            'certs' => 'certsImage',
            'followed',
        ];
    }

}
